﻿app.controller('seachpolygonController',function PostsController($scope, $filter, searchPolygonFactory, $http ) {
   
    $scope.loading = false;
   
    $scope.success = true;

    $scope.ResultLists = [];
   
    $scope.waitMessage = null;

    $scope.closeDialog = function () {
        $scope.message = null;
    };

    $scope.Filter1 = [];
    $scope.SelectedFilter1 = null;
    $scope.SelectedFilter1val = null;
    $scope.Filter1Autotcomplete = [];

    $scope.Filter2 = [];
    $scope.SelectedFilter2 = null;
    $scope.SelectedFilter2val = null;
    $scope.Filter2Autotcomplete = [];

    $scope.Filter3 = [];
    $scope.SelectedFilter3 = null;
    $scope.SelectedFilter3val = null;
    $scope.Filter3Autotcomplete = [];


    $scope.SelectedDrpSearchBy = null;
    $scope.DrpSearchBy = [];
    
    searchPolygonFactory.getall().success(function (response) {

        if (response) {
           /* debugger;*/
           
            $scope.message = response;
            $scope.success = true;
            $scope.DrpSearchBy = response;
            //angular.copy($scope.Polygon);

           
          
           
          
        }
        $scope.waitMessage = null;
    });

    $scope.closeDialog = function () {
        $scope.message = null;
    };
  
    $scope.drpChange = function (pTemplate) {
        //Your logic
        
        if (pTemplate == null)
        {
            $scope.clear();
           

            
        } else
        {
            var words1 = pTemplate.Filter1Fields.split(';');
            $scope.Filter1 = [];
            for (var i = 0; i < words1.length; i++) {
                $scope.Filter1.push(words1[i]);
            }

            var words2 = pTemplate.Filter2Fields.split(';');
            $scope.Filter2=[];
            for (var i = 0; i < words2.length; i++) {
                $scope.Filter2.push(words2[i]);
            }

            var words3 = pTemplate.Filter3Fields.split(';');
            $scope.Filter3 = [];
            for (var i = 0; i < words3.length; i++) {
                $scope.Filter3.push(words3[i]);
            }
        }
    };
    $scope.typeahead = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];
    $scope.SelectedFilter1FN = function (query) {
        $http.post("/Hetz/igisHandler.ashx?action=GetAutoCompletefltr"
          , {SelectedFilter1: $scope.SelectedFilter1,SelectedFilter1val: $scope.SelectedFilter1val,SelectedDrpSearchBy: $scope.SelectedDrpSearchBy
          }).success(function (response) {
              if (response) {                  
                  $scope.message = response;
                  $scope.success = true;
                  $scope.Filter1Autotcomplete = response;
              }         
          });
        return $.map($scope.Filter1Autotcomplete, function (country) {
            return country ;
        });
    }
    $scope.SelectedFilter2FN = function (query) {
        $http.post("/Hetz/igisHandler.ashx?action=GetAutoCompletefltr"
          , {
              SelectedFilter2: $scope.SelectedFilter2, SelectedFilter2val: $scope.SelectedFilter2val, SelectedDrpSearchBy: $scope.SelectedDrpSearchBy
          }).success(function (response) {
              if (response) {
                  $scope.message = response;
                  $scope.success = true;
                  $scope.Filter2Autotcomplete = response;
              }
          });
        return $.map($scope.Filter2Autotcomplete, function (country) {
            return country;
        });
    }
    $scope.SelectedFilter3FN = function (query) {
        $http.post("/Hetz/igisHandler.ashx?action=GetAutoCompletefltr"
          , {
              SelectedFilter3: $scope.SelectedFilter3, SelectedFilter3val: $scope.SelectedFilter3val, SelectedDrpSearchBy: $scope.SelectedDrpSearchBy
          }).success(function (response) {
              if (response) {
                  $scope.message = response;
                  $scope.success = true;
                  $scope.Filter3Autotcomplete = response;
              }
          });
        return $.map($scope.Filter3Autotcomplete, function (country) {
            return country;
        });
    }
    $scope.search = function () {
        $scope.waitMessage = "מחפש ";
       // var Polyg = this.Polygon;
        $http.post("/Hetz/igisHandler.ashx?action=GetPolygonSearch"
            , {SelectedFilter1 :$scope.SelectedFilter1,SelectedFilter2: $scope.SelectedFilter2, SelectedFilter3 :$scope.SelectedFilter3,
            SelectedFilter1val: $scope.SelectedFilter1val, SelectedFilter2val: $scope.SelectedFilter2val,SelectedFilter3val: $scope.SelectedFilter3val
                ,SelectedDrpSearchBy: $scope.SelectedDrpSearchBy }).success(function (response) {
           
            if (response) {
              
                
                    $scope.message = response;
                    $scope.success = true;
                    $scope.ResultLists = response;

                    //angular.copy($scope.Polygon);
                  
                    
                   
            }
            $scope.waitMessage = null;
        });
    };

    $scope.clear = function () {
        $scope.SelectedDrpSearchBy = null;
        $scope.Filter1 = null;
        $scope.Filter2 = null;
        $scope.Filter3 = null;

        $scope.SelectedFilter1 = null;
        $scope.SelectedFilter2 = null;
        $scope.SelectedFilter3 = null;
    }
 
  

    


   
   


    


});